package pers.cnzdy.tutorial.Chapter5_UI.Section3_Event;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import pers.cnzdy.tutorial.R;

public class EventActivity extends AppCompatActivity
        implements View.OnClickListener {

    static final String TAG = "EventActivity";

    private Button btn_answer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_life_cycle);

        btn_answer = findViewById(R.id.btn_answer);

        // 2. Java匿名类
        btn_answer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Java匿名类处理事件响应");
            }
        });

        btn_answer.setOnClickListener(new AnswerListener());
        btn_answer.setOnClickListener(this);
    }

    // 1. 界面控件属性
    public void startAnswerActivity(View view){
        Log.i(TAG, "界面控件属性处理事件响应");

        String message = "事件处理";
        Toast.makeText(EventActivity.this,
                message, Toast.LENGTH_SHORT).show();
    }

      // 3.	内部类
    class AnswerListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Log.i(TAG, "内部类处理事件响应");
        }
    }

      // 4.	Activity自身类
    @Override
    public void onClick(View v) {
        Log.i(TAG, "Activity自身类处理事件响应  " +
                (R.id.btn_answer == v.getId()));
    }
}

// 5. 外部类
class EventListener implements View.OnClickListener {
    /*通过事件监听器的成员变量及构造方法，事件源可以传递信息给事件监听器*/
    private Activity activity;
    private Button btn_answer;

    public EventListener(Activity activity, Button btn_answer) {
        this.activity = activity;
        this.btn_answer = btn_answer;
    }

    @Override
    public void onClick(View v) {
        Log.i("EventActivity", "外部类类处理事件响应  " +
                (R.id.btn_answer == v.getId()));
    }
}

